home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 15 / CU Amiga Magazine's Super CD-ROM 15 (1997)(EMAP Images)(GB)[!][issue 1997-10].iso / CUCD / Graphics / Ghostscript / source / gdevpbm.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-09-11  |  20.3 KB  |  683 lines

  1. /* Copyright (C) 1992, 1996 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of Aladdin Ghostscript.
  4.   
  5.   Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author
  6.   or distributor accepts any responsibility for the consequences of using it,
  7.   or for whether it serves any particular purpose or works at all, unless he
  8.   or she says so in writing.  Refer to the Aladdin Ghostscript Free Public
  9.   License (the "License") for full details.
  10.   
  11.   Every copy of Aladdin Ghostscript must include a copy of the License,
  12.   normally in a plain ASCII text file named PUBLIC.  The License grants you
  13.   the right to copy, modify and redistribute Aladdin Ghostscript, but only
  14.   under certain conditions described in the License.  Among other things, the
  15.   License requires that the copyright notice and this notice be preserved on
  16.   all copies.
  17. */
  18.  
  19. /* gdevpbm.c */
  20. /* Portable Bit/Gray/PixMap drivers */
  21. #include "gdevprn.h"
  22. #include "gscdefs.h"
  23. #include "gxlum.h"
  24.  
  25. /* Thanks are due to Jos Vos (jos@bull.nl) for an earlier P*M driver, */
  26. /* on which this one is based. */
  27.  
  28. /*
  29.  * There are 6 (pairs of) drivers here:
  30.  *    pbm[raw] - outputs PBM (black and white).
  31.  *    pgm[raw] - outputs PGM (gray-scale).
  32.  *    pgnm[raw] - outputs PBM if the page contains only black and white,
  33.  *      otherwise PGM.
  34.  *    ppm[raw] - outputs PPM (RGB).
  35.  *    pnm[raw] - outputs PBM if the page contains only black and white,
  36.  *      otherwise PGM if the page contains only gray shades,
  37.  *      otherwise PPM.
  38.  *    pkm[raw] - computes internally in CMYK, outputs PPM (RGB).
  39.  */
  40.  
  41. /*
  42.  * The code here is designed to work with variable depths for PGM and PPM.
  43.  * The code will work with any of the values in brackets, but the
  44.  * Ghostscript imager requires that depth be a power of 2 or be 24,
  45.  * so the actual allowed values are more limited.
  46.  *    pgm,pgnm: 1, 2, 4, 8, 16.  [1-16]
  47.  *    pgmraw,pgnmraw: 1, 2, 4, 8.  [1-8]
  48.  *    ppm,pnm: 4(3x1), 8(3x2), 16(3x5), 24(3x8), 32(3x10).  [3-32]
  49.  *    ppmraw,pnmraw: 4(3x1), 8(3x2), 16(3x5), 24(3x8).  [3-24]
  50.  *    pkm, pkmraw: 4(4x1), 8(4x2), 16(4x4), 32(4x8).  [4-32]
  51.  */
  52.  
  53. /* Structure for P*M devices, which extend the generic printer device. */
  54.  
  55. #define MAX_COMMENT 70            /* max user-supplied comment */
  56. struct gx_device_pbm_s {
  57.     gx_device_common;
  58.     gx_prn_device_common;
  59.     /* Additional state for P*M devices */
  60.     char magic;            /* n for "Pn" */
  61.     char comment[MAX_COMMENT + 1];    /* comment for head of file */
  62.     byte is_raw;            /* 1 if raw format, 0 if plain */
  63.     byte optimize;            /* 1 if optimization OK, 0 if not */
  64.     byte uses_color;        /* 0 if image is black and white, */
  65.                     /* 1 if gray (PGM or PPM only), */
  66.                     /* 2 or 3 if colored (PPM only) */
  67.     int alpha_text;            /* # of alpha bits for text (1,2,4) */
  68.     int alpha_graphics;        /* ditto for graphics (1,2,4) */
  69. };
  70. typedef struct gx_device_pbm_s gx_device_pbm;
  71.  
  72. #define bdev ((gx_device_pbm *)pdev)
  73.  
  74. /* ------ The device descriptors ------ */
  75.  
  76. /*
  77.  * Default X and Y resolution.
  78.  */
  79. #define X_DPI 72
  80. #define Y_DPI 72
  81.  
  82. /* Macro for generating P*M device descriptors. */
  83. #define pbm_prn_device(procs, dev_name, magic, is_raw, num_comp, depth, max_gray, max_rgb, optimize, print_page)\
  84. {    prn_device_body(gx_device_pbm, procs, dev_name,\
  85.       DEFAULT_WIDTH_10THS, DEFAULT_HEIGHT_10THS, X_DPI, Y_DPI,\
  86.       0, 0, 0, 0,\
  87.       num_comp, depth, max_gray, max_rgb, max_gray + 1, max_rgb + 1,\
  88.       print_page),\
  89.     magic,\
  90.      { 0 },\
  91.     is_raw,\
  92.     optimize,\
  93.     0, 1, 1\
  94. }
  95.  
  96. /* For all but PBM, we need our own color mapping and alpha procedures. */
  97. private dev_proc_map_rgb_color(pgm_map_rgb_color);
  98. private dev_proc_map_rgb_color(ppm_map_rgb_color);
  99. private dev_proc_map_color_rgb(pgm_map_color_rgb);
  100. private dev_proc_map_color_rgb(ppm_map_color_rgb);
  101. private dev_proc_map_cmyk_color(pkm_map_cmyk_color);
  102. private dev_proc_map_color_rgb(pkm_map_color_rgb);
  103. private dev_proc_put_params(ppm_put_params);
  104. private dev_proc_get_alpha_bits(ppm_get_alpha_bits);
  105.  
  106. /* We need to initialize uses_color when opening the device, */
  107. /* and after each showpage. */
  108. private dev_proc_open_device(ppm_open);
  109. private dev_proc_output_page(ppm_output_page);
  110.  
  111. /* And of course we need our own print-page routines. */
  112. private dev_proc_print_page(pbm_print_page);
  113. private dev_proc_print_page(pgm_print_page);
  114. private dev_proc_print_page(ppm_print_page);
  115. private dev_proc_print_page(pkm_print_page);
  116.  
  117. /* The device procedures */
  118.  
  119. private gx_device_procs pbm_procs =
  120.     prn_procs(gdev_prn_open, ppm_output_page, gdev_prn_close);
  121.  
  122. /* See gdevprn.h for the template for the following. */
  123. #define pgpm_procs(p_map_rgb_color, p_map_color_rgb, p_map_cmyk_color) {\
  124.     ppm_open, NULL, NULL, ppm_output_page, gdev_prn_close,\
  125.     p_map_rgb_color, p_map_color_rgb, NULL, NULL, NULL, NULL, NULL, NULL,\
  126.     gdev_prn_get_params, ppm_put_params,\
  127.     p_map_cmyk_color, NULL, NULL, NULL, gx_page_device_get_page_device,\
  128.     ppm_get_alpha_bits\
  129. }
  130.  
  131. private gx_device_procs pgm_procs =
  132.     pgpm_procs(pgm_map_rgb_color, pgm_map_color_rgb, NULL);
  133. private gx_device_procs ppm_procs =
  134.     pgpm_procs(ppm_map_rgb_color, ppm_map_color_rgb, NULL);
  135. private gx_device_procs pkm_procs =
  136.     pgpm_procs(NULL, pkm_map_color_rgb, pkm_map_cmyk_color);
  137.  
  138. /* The device descriptors themselves */
  139. gx_device_pbm far_data gs_pbm_device =
  140.   pbm_prn_device(pbm_procs, "pbm", '1', 0, 1, 1, 1, 0, 0,
  141.          pbm_print_page);
  142. gx_device_pbm far_data gs_pbmraw_device =
  143.   pbm_prn_device(pbm_procs, "pbmraw", '4', 1, 1, 1, 1, 1, 0,
  144.          pbm_print_page);
  145. gx_device_pbm far_data gs_pgm_device =
  146.   pbm_prn_device(pgm_procs, "pgm", '2', 0, 1, 8, 255, 0, 0,
  147.          pgm_print_page);
  148. gx_device_pbm far_data gs_pgmraw_device =
  149.   pbm_prn_device(pgm_procs, "pgmraw", '5', 1, 1, 8, 255, 0, 0,
  150.          pgm_print_page);
  151. gx_device_pbm far_data gs_pgnm_device =
  152.   pbm_prn_device(pgm_procs, "pgnm", '2', 0, 1, 8, 255, 0, 1,
  153.          pgm_print_page);
  154. gx_device_pbm far_data gs_pgnmraw_device =
  155.   pbm_prn_device(pgm_procs, "pgnmraw", '5', 1, 1, 8, 255, 0, 1,
  156.          pgm_print_page);
  157. gx_device_pbm far_data gs_ppm_device =
  158.   pbm_prn_device(ppm_procs, "ppm", '3', 0, 3, 24, 255, 255, 0,
  159.          ppm_print_page);
  160. gx_device_pbm far_data gs_ppmraw_device =
  161.   pbm_prn_device(ppm_procs, "ppmraw", '6', 1, 3, 24, 255, 255, 0,
  162.          ppm_print_page);
  163. gx_device_pbm far_data gs_pnm_device =
  164.   pbm_prn_device(ppm_procs, "pnm", '3', 0, 3, 24, 255, 255, 1,
  165.          ppm_print_page);
  166. gx_device_pbm far_data gs_pnmraw_device =
  167.   pbm_prn_device(ppm_procs, "pnmraw", '6', 1, 3, 24, 255, 255, 1,
  168.          ppm_print_page);
  169. gx_device_pbm far_data gs_pkm_device =
  170.   pbm_prn_device(pkm_procs, "pkm", '3', 0, 4, 4, 1, 1, 0,
  171.          pkm_print_page);
  172. gx_device_pbm far_data gs_pkmraw_device =
  173.   pbm_prn_device(pkm_procs, "pkmraw", '6', 1, 4, 4, 1, 1, 0,
  174.          pkm_print_page);
  175.  
  176. /* ------ Initialization ------ */
  177.  
  178. private int
  179. ppm_open(gx_device *pdev)
  180. {    bdev->uses_color = 0;
  181.     return gdev_prn_open(pdev);
  182. }
  183.  
  184. /* Print a page, and reset uses_color if this is a showpage. */
  185. private int
  186. ppm_output_page(gx_device *pdev, int num_copies, int flush)
  187. {    int code = gdev_prn_output_page(pdev, num_copies, flush);
  188.     if ( code < 0 )
  189.       return code;
  190.     if ( flush )
  191.       bdev->uses_color = 0;
  192.     return code;
  193. }
  194.  
  195. /* ------ Color mapping routines ------ */
  196.  
  197. /* Map an RGB color to a PGM gray value. */
  198. /* Keep track of whether the image is black-and-white or gray. */
  199. private gx_color_index
  200. pgm_map_rgb_color(gx_device *pdev, ushort r, ushort g, ushort b)
  201. {    /* We round the value rather than truncating it. */
  202.     gx_color_value gray =
  203.         ((r * (ulong)lum_red_weight) +
  204.          (g * (ulong)lum_green_weight) +
  205.          (b * (ulong)lum_blue_weight) +
  206.          (lum_all_weights / 2)) / lum_all_weights
  207.         * pdev->color_info.max_gray / gx_max_color_value;
  208.     if ( !(gray == 0 || gray == pdev->color_info.max_gray) )
  209.       bdev->uses_color = 1;
  210.     return gray;
  211. }
  212.  
  213. /* Map a PGM gray value back to an RGB color. */
  214. private int
  215. pgm_map_color_rgb(gx_device *dev, gx_color_index color, ushort prgb[3])
  216. {    gx_color_value gray =
  217.       color * gx_max_color_value / dev->color_info.max_gray;
  218.     prgb[0] = gray;
  219.     prgb[1] = gray;
  220.     prgb[2] = gray;
  221.     return 0;
  222. }
  223.  
  224. /* Map an RGB color to a PPM color tuple. */
  225. /* Keep track of whether the image is black-and-white, gray, or colored. */
  226. private gx_color_index
  227. ppm_map_rgb_color(gx_device *pdev, ushort r, ushort g, ushort b)
  228. {    uint bitspercolor = pdev->color_info.depth / 3;
  229.     ulong max_value = pdev->color_info.max_color;
  230.     gx_color_value rc = r * max_value / gx_max_color_value;
  231.     gx_color_value gc = g * max_value / gx_max_color_value;
  232.     gx_color_value bc = b * max_value / gx_max_color_value;
  233.     if ( rc == gc && gc == bc )        /* black-and-white or gray */
  234.     {    if ( !(rc == 0 || rc == max_value) )
  235.           bdev->uses_color |= 1;        /* gray */
  236.     }
  237.     else                        /* color */
  238.       bdev->uses_color = 2;
  239.     return ((((ulong)rc << bitspercolor) + gc) << bitspercolor) + bc;
  240. }
  241.  
  242. /* Map a PPM color tuple back to an RGB color. */
  243. private int
  244. ppm_map_color_rgb(gx_device *dev, gx_color_index color, ushort prgb[3])
  245. {    uint bitspercolor = dev->color_info.depth / 3;
  246.     uint colormask = (1 << bitspercolor) - 1;
  247.     uint max_rgb = dev->color_info.max_color;
  248.  
  249.     prgb[0] = ((color >> (bitspercolor * 2)) & colormask) *
  250.       (ulong)gx_max_color_value / max_rgb;
  251.     prgb[1] = ((color >> bitspercolor) & colormask) *
  252.       (ulong)gx_max_color_value / max_rgb;
  253.     prgb[2] = (color & colormask) *
  254.       (ulong)gx_max_color_value / max_rgb;
  255.     return 0;
  256. }
  257.  
  258. /* Map a CMYK color to a pixel value. */
  259. private gx_color_index
  260. pkm_map_cmyk_color(gx_device *pdev, ushort c, ushort m, ushort y, ushort k)
  261. {    uint bitspercolor = pdev->color_info.depth >> 2;
  262.     ulong max_value = pdev->color_info.max_color;
  263.     uint cc = c * max_value / gx_max_color_value;
  264.     uint mc = m * max_value / gx_max_color_value;
  265.     uint yc = y * max_value / gx_max_color_value;
  266.     uint kc = k * max_value / gx_max_color_value;
  267.     gx_color_index color =
  268.       ((((((ulong)cc << bitspercolor) + mc) << bitspercolor) + yc)
  269.        << bitspercolor) + kc;
  270.  
  271.     return (color == gx_no_color_index ? color ^ 1 : color);
  272. }
  273.  
  274. /* Map a CMYK pixel value to RGB. */
  275. private int
  276. pkm_map_color_rgb(gx_device *dev, gx_color_index color, gx_color_value rgb[3])
  277. {    gx_color_index cshift = color;
  278.     int bpc = dev->color_info.depth >> 2;
  279.     uint mask = (1 << bpc) - 1;
  280.     uint max_value = dev->color_info.max_color;
  281.     uint c, m, y, k;
  282.  
  283.     k = cshift & mask;  cshift >>= bpc;
  284.     y = cshift & mask;  cshift >>= bpc;
  285.     m = cshift & mask;
  286.     c = cshift >> bpc;
  287. #define cvalue(c)\
  288.   ((gx_color_value)((ulong)(c) * gx_max_color_value / max_value))
  289.     /* We use our improved conversion rule.... */
  290.     rgb[0] = cvalue((max_value - c) * (max_value - k) / max_value);
  291.     rgb[1] = cvalue((max_value - m) * (max_value - k) / max_value);
  292.     rgb[2] = cvalue((max_value - y) * (max_value - k) / max_value);
  293. #undef cvalue
  294.     return 0;
  295. }
  296.  
  297. /* ------ Alpha capability ------ */
  298.  
  299. /* Put parameters. */
  300. private int
  301. ppm_put_alpha_param(gs_param_list *plist, gs_param_name param_name, int *pa,
  302.   bool alpha_ok)
  303. {    int code = param_read_int(plist, param_name, pa);
  304.     switch ( code )
  305.     {
  306.     case 0:
  307.         switch ( *pa )
  308.           {
  309.           case 1:
  310.             return 0;
  311.           case 2: case 4:
  312.             if ( alpha_ok )
  313.               return 0;
  314.           default:
  315.             code = gs_error_rangecheck;
  316.           }
  317.     default:
  318.         param_signal_error(plist, param_name, code);
  319.     case 1:
  320.         ;
  321.     }
  322.     return code;
  323. }
  324. private int
  325. ppm_put_params(gx_device *pdev, gs_param_list *plist)
  326. {    gx_device_color_info save_info;
  327.     int ncomps = pdev->color_info.num_components;
  328.     int bpc = pdev->color_info.depth / ncomps;
  329.     int ecode = 0;
  330.     int code;
  331.     int atext = bdev->alpha_text, agraphics = bdev->alpha_graphics;
  332.     bool alpha_ok;
  333.     long v;
  334.     const char _ds *vname;
  335.  
  336.     save_info = pdev->color_info;
  337.     if ( (code = param_read_long(plist, (vname = "GrayValues"), &v)) != 1 ||
  338.          (code = param_read_long(plist, (vname = "RedValues"), &v)) != 1 ||
  339.          (code = param_read_long(plist, (vname = "GreenValues"), &v)) != 1 ||
  340.          (code = param_read_long(plist, (vname = "BlueValues"), &v)) != 1
  341.        )
  342.       {    if ( code < 0 )
  343.           ecode = code;
  344.         else if ( v < 2 || v > (bdev->is_raw || ncomps > 1 ? 256 : 65536L) )
  345.           param_signal_error(plist, vname,
  346.                      ecode = gs_error_rangecheck);
  347.         else if ( v == 2 )
  348.           bpc = 1;
  349.         else if ( v <= 4 )
  350.           bpc = 2;
  351.         else if ( v <= 16 )
  352.           bpc = 4;
  353.         else if ( v <= 32 && ncomps == 3 )
  354.           bpc = 5;
  355.         else if ( v <= 256 )
  356.           bpc = 8;
  357.           else
  358.           bpc = 16;
  359.           if ( ecode >= 0 )
  360.           { static const byte depths[4][16] = {
  361.               { 1, 2, 0, 4, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 16 },
  362.               { 0 },
  363.               { 4, 8, 0, 16, 16, 0, 0, 24 },
  364.               { 4, 8, 0, 16, 0, 0, 0, 24 },
  365.             };
  366.             pdev->color_info.depth = depths[ncomps - 1][bpc - 1];
  367.             pdev->color_info.max_gray = pdev->color_info.max_color =
  368.               (pdev->color_info.dither_grays =
  369.                pdev->color_info.dither_colors = (int)v) - 1;
  370.           }
  371.       }
  372.     alpha_ok = bpc >= 5;
  373.     if ( (code = ppm_put_alpha_param(plist, "TextAlphaBits", &bdev->alpha_text, alpha_ok)) < 0 )
  374.       ecode = code;
  375.     if ( (code = ppm_put_alpha_param(plist, "GraphicsAlphaBits", &bdev->alpha_graphics, alpha_ok)) < 0 )
  376.       ecode = code;
  377.     if ( (code = ecode) < 0 ||
  378.          (code = gdev_prn_put_params(pdev, plist)) < 0
  379.        )
  380.       { bdev->alpha_text = atext;
  381.         bdev->alpha_graphics = agraphics;
  382.         pdev->color_info = save_info;
  383.       }
  384.     return code;
  385. }
  386.  
  387. /* Get the number of alpha bits. */
  388. private int
  389. ppm_get_alpha_bits(gx_device *pdev, graphics_object_type type)
  390. {    return (type == go_text ? bdev->alpha_text : bdev->alpha_graphics);
  391. }
  392.  
  393. /* ------ Internal routines ------ */
  394.  
  395. /* Print a page using a given row printing routine. */
  396. private int
  397. pbm_print_page_loop(gx_device_printer *pdev, char magic, FILE *pstream,
  398.   int (*row_proc)(P4(gx_device_printer *, byte *, int, FILE *)))
  399. {    uint raster = gdev_prn_raster(pdev);
  400.     byte *data = (byte *)gs_malloc(raster, 1, "pbm_begin_page");
  401.     int lnum = 0;
  402.     int code = 0;
  403.     if ( data == 0 )
  404.       return_error(gs_error_VMerror);
  405.     fprintf(pstream, "P%c\n", magic);
  406.     if ( bdev->comment[0] )
  407.       fprintf(pstream, "# %s\n", bdev->comment);
  408.     else
  409.       fprintf(pstream, "# Image generated by %s (device=%s)\n",
  410.           gs_product, pdev->dname);
  411.     fprintf(pstream, "%d %d\n", pdev->width, pdev->height);
  412.     switch ( magic )
  413.     {
  414.     case '1':        /* pbm */
  415.     case '4':        /* pbmraw */
  416.         break;
  417.     default:
  418.         fprintf(pstream, "%d\n", pdev->color_info.max_gray);
  419.     }
  420.     for ( ; lnum < pdev->height; lnum++ )
  421.     {    byte *row;
  422.         code = gdev_prn_get_bits(pdev, lnum, data, &row);
  423.         if ( code < 0 ) break;
  424.         code = (*row_proc)(pdev, row, pdev->color_info.depth, pstream);
  425.         if ( code < 0 ) break;
  426.     }
  427.     gs_free((char *)data, raster, 1, "pbm_print_page_loop");
  428.     return (code < 0 ? code : 0);
  429. }
  430.  
  431. /* ------ Individual page printing routines ------ */
  432.  
  433. /* Print a monobit page. */
  434. private int
  435. pbm_print_row(gx_device_printer *pdev, byte *data, int depth,
  436.   FILE *pstream)
  437. {    if ( bdev->is_raw )
  438.       fwrite(data, 1, (pdev->width + 7) >> 3, pstream);
  439.     else
  440.     {    byte *bp;
  441.         uint x, mask;
  442.         for ( bp = data, x = 0, mask = 0x80; x < pdev->width; )
  443.         {    putc((*bp & mask ? '1' : '0'), pstream);
  444.             if ( ++x == pdev->width || !(x & 63) )
  445.               putc('\n', pstream);
  446.             if ( (mask >>= 1) == 0 )
  447.               bp++, mask = 0x80;
  448.         }
  449.     }
  450.     return 0;
  451. }
  452. private int
  453. pbm_print_page(gx_device_printer *pdev, FILE *pstream)
  454. {    return pbm_print_page_loop(pdev, bdev->magic, pstream, pbm_print_row);
  455. }
  456.  
  457. /* Print a gray-mapped page. */
  458. private int
  459. pgm_print_row(gx_device_printer *pdev, byte *data, int depth,
  460.   FILE *pstream)
  461. {    /* Note that bpp <= 8 for raw format, bpp <= 16 for plain. */
  462.     uint mask = (1 << depth) - 1;
  463.     byte *bp;
  464.     uint x;
  465.     int shift;
  466.     if ( bdev->is_raw && depth == 8 )
  467.       fwrite(data, 1, pdev->width, pstream);
  468.     else
  469.       for ( bp = data, x = 0, shift = 8 - depth; x < pdev->width; )
  470.     {    uint pixel;
  471.         if ( shift < 0 )    /* bpp = 16 */
  472.            {    pixel = ((uint)*bp << 8) + bp[1];
  473.             bp += 2;
  474.            }
  475.         else
  476.            {    pixel = (*bp >> shift) & mask;
  477.             if ( (shift -= depth) < 0 )
  478.               bp++, shift += 8;
  479.            }
  480.         ++x;
  481.         if ( bdev->is_raw )
  482.           putc(pixel, pstream);
  483.         else
  484.           fprintf(pstream, "%d%c", pixel,
  485.               (x == pdev->width || !(x & 15) ? '\n' : ' '));
  486.     }
  487.     return 0;
  488. }
  489. private int
  490. pxm_pbm_print_row(gx_device_printer *pdev, byte *data, int depth,
  491.   FILE *pstream)
  492. {    /* Compress a PGM or PPM row to a PBM row. */
  493.     /* This doesn't have to be very fast. */
  494.     /* Note that we have to invert the data as well. */
  495.     int delta = (depth + 7) >> 3;
  496.     byte *src = data + delta - 1;        /* always big-endian */
  497.     byte *dest = data;
  498.     int x;
  499.     byte out_mask = 0x80;
  500.     byte out = 0;
  501.     if ( depth >= 8 )
  502.       {    /* One or more bytes per source pixel. */
  503.         for ( x = 0; x < pdev->width; x++, src += delta )
  504.           {    if ( !(*src & 1) )
  505.               out |= out_mask;
  506.             out_mask >>= 1;
  507.             if ( !out_mask )
  508.               out_mask = 0x80,
  509.               *dest++ = out,
  510.               out = 0;
  511.           }
  512.       }
  513.     else
  514.       {    /* Multiple source pixels per byte. */
  515.         byte in_mask = 0x100 >> depth;
  516.         for ( x = 0; x < pdev->width; x++ )
  517.           {    if ( !(*src & in_mask) )
  518.               out |= out_mask;
  519.             in_mask >>= depth;
  520.             if ( !in_mask )
  521.               in_mask = 0x100 >> depth,
  522.               src++;
  523.             out_mask >>= 1;
  524.             if ( !out_mask )
  525.               out_mask = 0x80,
  526.               *dest++ = out,
  527.               out = 0;
  528.           }
  529.     }
  530.     if ( out_mask != 0x80 )
  531.       *dest = out;
  532.     return pbm_print_row(pdev, data, 1, pstream);
  533. }
  534. private int
  535. pgm_print_page(gx_device_printer *pdev, FILE *pstream)
  536. {    return (bdev->uses_color == 0 && bdev->optimize ?
  537.         pbm_print_page_loop(pdev, bdev->magic - 1, pstream,
  538.                     pxm_pbm_print_row) :
  539.         pbm_print_page_loop(pdev, bdev->magic, pstream,
  540.                     pgm_print_row) );
  541. }
  542.  
  543. /* Print a color-mapped page. */
  544. private int
  545. ppgm_print_row(gx_device_printer *pdev, byte *data, int depth,
  546.   FILE *pstream, bool color)
  547. {    /* If color=false, write only one value per pixel; */
  548.     /* if color=true, write 3 values per pixel. */
  549.     /* Note that depth <= 24 for raw format, depth <= 32 for plain. */
  550.     uint bpe = depth / 3;    /* bits per r/g/b element */
  551.     uint mask = (1 << bpe) - 1;
  552.     byte *bp;
  553.     uint x;
  554.     uint eol_mask = (color ? 7 : 15);
  555.     int shift;
  556.     if ( bdev->is_raw && depth == 24 && color )
  557.       fwrite(data, 1, pdev->width * (depth / 8), pstream);
  558.     else
  559.       for ( bp = data, x = 0, shift = 8 - depth; x < pdev->width; )
  560.     {    bits32 pixel = 0;
  561.         uint r, g, b;
  562.         switch ( depth >> 3 )
  563.            {
  564.         case 4:
  565.             pixel = (bits32)*bp << 24; bp++;
  566.             /* falls through */
  567.         case 3:
  568.             pixel += (bits32)*bp << 16; bp++;
  569.             /* falls through */
  570.         case 2:
  571.             pixel += (uint)*bp << 8; bp++;
  572.             /* falls through */
  573.         case 1:
  574.             pixel += *bp; bp++;
  575.             break;
  576.         case 0:            /* bpp == 4, bpe == 1 */
  577.             pixel = *bp >> shift;
  578.             if ( (shift -= depth) < 0 )
  579.               bp++, shift += 8;
  580.             break;
  581.            }
  582.         ++x;
  583.         b = pixel & mask;  pixel >>= bpe;
  584.         g = pixel & mask;  pixel >>= bpe;
  585.         r = pixel & mask;
  586.         if ( bdev->is_raw )
  587.         {    if ( color )
  588.             {    putc(r, pstream);
  589.                 putc(g, pstream);
  590.             }
  591.             putc(b, pstream);
  592.         }
  593.         else
  594.         {    if ( color )
  595.               fprintf(pstream, "%d %d ", r, g);
  596.             fprintf(pstream, "%d%c", b,
  597.                 (x == pdev->width || !(x & eol_mask) ?
  598.                  '\n' : ' '));
  599.         }
  600.     }
  601.     return 0;
  602. }
  603. private int
  604. ppm_print_row(gx_device_printer *pdev, byte *data, int depth,
  605.   FILE *pstream)
  606. {    return ppgm_print_row(pdev, data, depth, pstream, true);
  607. }
  608. private int
  609. ppm_pgm_print_row(gx_device_printer *pdev, byte *data, int depth,
  610.   FILE *pstream)
  611. {    return ppgm_print_row(pdev, data, depth, pstream, false);
  612. }
  613. private int
  614. ppm_print_page(gx_device_printer *pdev, FILE *pstream)
  615. {    return (bdev->uses_color >= 2 || !bdev->optimize ?
  616.         pbm_print_page_loop(pdev, bdev->magic, pstream,
  617.                     ppm_print_row) :
  618.         bdev->uses_color == 1 ?
  619.         pbm_print_page_loop(pdev, bdev->magic - 1, pstream,
  620.                     ppm_pgm_print_row) :
  621.         pbm_print_page_loop(pdev, bdev->magic - 2, pstream,
  622.                     pxm_pbm_print_row) );
  623. }
  624.  
  625. /* Print a faux CMYK page. */
  626. private int
  627. pkm_print_row(gx_device_printer *pdev, byte *data, int depth,
  628.   FILE *pstream)
  629. {    byte *bp;
  630.     uint x;
  631.     int shift;
  632.     ulong max_value = pdev->color_info.max_color;
  633.     uint mask = (depth >= 8 ? 0xff : (1 << depth) - 1);
  634.  
  635.     for ( bp = data, x = 0, shift = 8 - depth; x < pdev->width; )
  636.     {    bits32 pixel = 0;
  637.         gx_color_value rgb[3];
  638.         uint r, g, b;
  639.  
  640.         switch ( depth >> 3 )
  641.            {
  642.         case 4:
  643.             pixel = (bits32)*bp << 24; bp++;
  644.             /* falls through */
  645.         case 3:
  646.             pixel += (bits32)*bp << 16; bp++;
  647.             /* falls through */
  648.         case 2:
  649.             pixel += (uint)*bp << 8; bp++;
  650.             /* falls through */
  651.         case 1:
  652.             pixel += *bp; bp++;
  653.             break;
  654.         case 0:            /* bpp == 4 */
  655.             pixel = (*bp >> shift) & mask;
  656.             if ( (shift -= depth) < 0 )
  657.               bp++, shift += 8;
  658.             break;
  659.            }
  660.         ++x;
  661.         pkm_map_color_rgb((gx_device *)pdev, pixel, rgb);
  662.         r = rgb[0] * max_value / gx_max_color_value;
  663.         g = rgb[1] * max_value / gx_max_color_value;
  664.         b = rgb[2] * max_value / gx_max_color_value;
  665.         if ( bdev->is_raw )
  666.         {    putc(r, pstream);
  667.             putc(g, pstream);
  668.             putc(b, pstream);
  669.         }
  670.         else
  671.         {    fprintf(pstream, "%d %d %d%c", r, g, b,
  672.                 (x == pdev->width || !(x & 7) ?
  673.                  '\n' : ' '));
  674.         }
  675.     }
  676.     return 0;
  677. }
  678. private int
  679. pkm_print_page(gx_device_printer *pdev, FILE *pstream)
  680. {    return pbm_print_page_loop(pdev, bdev->magic, pstream,
  681.                    pkm_print_row);
  682. }
  683.